local H = require "packages/new_heg/util"
local HC = require "packages/new_hegcomplements/util"
local U = require "packages/utility/utility"
local extension = Package:new("olheg")
extension.extensionName = "new_hegcomplements"
extension.game_modes_whitelist = { 'ex_heg_mode', 'mul_heg_mode' }

Fk:loadTranslationTable {
  ["olheg"] = "OL",
  ["ol_hc_heg"] = "OL",
}

local huangzhong = General(extension, "ol_hc_heg__n_huangzhong", "shu", 4)

local liegong = fk.CreateTriggerSkill{
  name = "ol_hc__liegong",
  anim_type = "offensive",
  events = {fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    if not (target == player and player:hasSkill(self) and player.phase == Player.Play and data.card.trueName == "slash") then return end
    local room = player.room
    local to = room:getPlayerById(data.to)
    local num = #to:getCardIds(Player.Hand)
    return num <= player:getAttackRange() or num >= player.hp
  end,
  on_cost =function (self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, data, "#ol_hc__liegong::"..data.to)
  end,
  on_use = function(self, event, target, player, data)
    data.disresponsiveList = data.disresponsiveList or {}
    table.insert(data.disresponsiveList, data.to)
  end,
}

local liegong_range = fk.CreateAttackRangeSkill{
  name = "#ol_hc__liegong_range",
  correct_func = function(self, from, to)
    if from:hasShownSkill(liegong.name) then
      for _, p in ipairs(Fk:currentRoom().alive_players) do
        if string.find(p.general, "lord") and p:hasSkill("shouyue") and p.kingdom == from.kingdom then
          return 1
        end
      end
    end
    return 0
  end,
}

local liegong_targetmod = fk.CreateTargetModSkill{
  name = "#ol_hc__liegong_targetmod",
    bypass_distances =  function(self, player, skill, card, target)
    if skill.trueName == "slash_skill" and player:hasShownSkill(liegong.name) then
      return card and target and player:distanceTo(target) <= card.number
    end
  end,
}

huangzhong:addCompanions("hs__n_weiyan")
liegong:addRelatedSkill(liegong_range)
liegong:addRelatedSkill(liegong_targetmod)
huangzhong:addSkill(liegong)

Fk:loadTranslationTable{
  ["ol_hc_heg__n_huangzhong"] = "黄忠",
  ["#ol_hc_heg__n_huangzhong"] = "老当益壮",
  ["illustrator:ol_hc_heg__n_huangzhong"] = "KayaK",

  ["ol_hc__liegong"] = "烈弓",
  [":ol_hc__liegong"] = "你使用【杀】对距离不大于此【杀】的点数的角色无距离限制。当你于出牌阶段内使用【杀】指定目标后，若其手牌数不小于你的体力值或不大于你的攻击范围，你可令其不能使用【闪】响应此【杀】。",

  ["#ol_hc__liegong"] = "你可发动〖烈弓〗，令 %dest 不能响应",

  ["$ol_hc__liegong1"] = "龙骨成镞，矢破苍穹。",
  ["$ol_hc__liegong2"] = "凤翎为羽，箭没坚城。",
  ["~ol_hc_heg__n_huangzhong"] = "末将有负主公重托……",
}

local masu = General(extension, "ol_hc_heg__n_masu", "shu", 3)
local sanyao = fk.CreateActiveSkill{
  name = "ol_hc__sanyao",
  anim_type = "offensive",
  card_num = 1,
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and not player:isNude()
  end,
  card_filter = function(self, to_select, selected, player)
    return #selected == 0 and not player:prohibitDiscard(Fk:getCardById(to_select))
  end,
  target_filter = function(self, to_select, selected, _, _, _, player)
    local target = Fk:currentRoom():getPlayerById(to_select)
    if #selected == 0 then
      return  to_select ~= player.id and(( target.hp > player.hp ) or (target:getHandcardNum() > player:getHandcardNum()))
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    room:throwCard(effect.cards, self.name, player, player)
    if target.dead then return end
    room:damage{
      from = player,
      to = target,
      damage = 1,
      skillName = self.name,
    }
  end
}
local zhiman = fk.CreateTriggerSkill{
  name = "ol_hc__zhiman",
  events = {fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.to ~= player
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#ol_hc__zhiman-invoke::"..data.to.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local target = data.to
    room:doIndicate(player.id, {target.id})
    if #target:getCardIds{Player.Equip, Player.Judge} > 0 then -- 开摆！
      local card = room:askForCardChosen(player, target, "ej", self.name)
      room:obtainCard(player.id, card, true, fk.ReasonPrey)
    end
    if H.compareKingdomWith(target, player) and player:getMark("@@ol_hc__zhiman_transform") == 0
      --and room:askForChoice(player, {"ol_hc__zhiman_transform::" .. target.id, "Cancel"}, self.name) ~= "Cancel" --OL不用经过马谡同意
      and not target.dead
      and room:askForChoice(target, {"transform_deputy", "Cancel"}, self.name) ~= "Cancel" then
        room:setPlayerMark(player, "@@ol_hc__zhiman_transform", 1)
        H.transformGeneral(room, target)
    end
    return true
  end
}

masu:addSkill(sanyao)
masu:addSkill(zhiman)

Fk:loadTranslationTable{
  ['ol_hc_heg__n_masu'] = '马谡',
  ["#ol_hc_heg__n_masu"] = "帷幄经谋",
  ["illustrator:ol_hc_heg__n_masu"] = "蚂蚁君",

  ["ol_hc__sanyao"] = "散谣",
  [":ol_hc__sanyao"] = "出牌阶段限一次，你可以弃置一张牌并选择一名体力值或者手牌数大于你的其他角色，你对其造成1点伤害。",
  ["ol_hc__zhiman"] = "制蛮",
  [":ol_hc__zhiman"] = "当你对其他角色造成伤害时，你可防止此伤害，你获得其装备区或判定区里的一张牌。若其与你势力相同，其选择是否变更。",

  ["#ol_hc__zhiman-invoke"] = "制蛮：你可以防止对 %dest 造成的伤害，获得其场上的一张牌。若其与你势力相同，其选择是否变更副将",
  --["ol_hc__zhiman_transform"] = "令%dest选择是否变更副将",
  ["@@ol_hc__zhiman_transform"] = "制蛮 已变更",

  ["$ol_hc__sanyao1"] = "三人成虎，事多有。",
  ["$ol_hc__sanyao2"] = "散谣惑敌，不攻自破！",
  ["$ol_hc__zhiman1"] = "兵法谙熟于心，取胜千里之外！",
  ["$ol_hc__zhiman2"] = "丞相多虑，且看我的！",
  ["~ol_hc_heg__n_masu"] = "败军之罪，万死难赎……" ,
}

local xusheng = General(extension, "ol_hc_heg__n_xusheng", "wu", 4)

local pojun = fk.CreateTriggerSkill{
  name = "ol_hc__pojun",
  anim_type = "offensive",
  events = {fk.TargetSpecified, fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and data.card and data.card.trueName == "slash" then
      if event == fk.TargetSpecified then
        local to = player.room:getPlayerById(data.to)
        return not to.dead and to.hp > 0 and not to:isNude()
      elseif event == fk.DamageCaused then
        return player:getHandcardNum() >= data.to:getHandcardNum() and
        #player:getCardIds(Player.Equip) >= #data.to:getCardIds(Player.Equip)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.DamageCaused or player.room:askForSkillInvoke(player, self.name, nil, "#ol_hc__pojun-invoke::"..data.to) then
      self.cost_data = {tos = {event == fk.DamageCaused and data.to.id or data.to}}
      return true
    end 
    
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TargetSpecified then
      local to = room:getPlayerById(data.to)
      local cards = room:askForCardsChosen(player, to, 1, to.hp, "he", self.name)
      to:addToPile("$ol_hc__pojun", cards, false, self.name)
    else
      data.damage = data.damage + 1
    end
  end,
}

local pojun_delay = fk.CreateTriggerSkill{
  name = "#ol_hc__pojun_delay",
  mute = true,
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    return #player:getPile("$ol_hc__pojun") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player.room:moveCardTo(player:getPile("$ol_hc__pojun"), Player.Hand, player, fk.ReasonPrey, "ol_hc__pojun")
  end,
}

xusheng:addCompanions("hs__n_dingfeng")
pojun:addRelatedSkill(pojun_delay)
xusheng:addSkill(pojun)

Fk:loadTranslationTable{
  ["ol_hc_heg__n_xusheng"] = "徐盛",
  ["#ol_hc_heg__n_xusheng"] = "江东的铁壁",
  ["illustrator:ol_hc_heg__n_xusheng"] = "铁杵文化",
  
  ["ol_hc__pojun"] = "破军",
  ["#ol_hc__pojun_delay"] = "破军",
  [":ol_hc__pojun"] = "当你使用【杀】指定一个目标后，你可以将其至多X张牌扣置于该角色的武将牌旁（X为其体力值），若如此做，当前回合结束时，其获得这些牌；当你使用【杀】对手牌数与装备区里的牌数均不大于你的角色造成伤害时，此伤害+1。",

  ["#ol_hc__pojun-invoke"] = "是否对 %dest 发动 破军",
  ["$ol_hc__pojun"] = "破军",

  ["$ol_hc__pojun1"] = "犯大吴疆土者，盛必击而破之！",
  ["$ol_hc__pojun2"] = "若敢来犯，必叫你大败而归！",
  ["~ol_hc_heg__n_xusheng"] = "盛只恨，不能再为主公，破敌致胜了。",
}

local hetaihou = General(extension, "ol_hc_heg__n_hetaihou", "qun", 3, 3, General.Female)

local ol__zhendu = fk.CreateTriggerSkill{
  name = "ol_hc__zhendu",
  anim_type = "offensive",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target.phase == Player.Play and not player:isKongcheng() and not target.dead
  end,
  on_cost = function(self, event, target, player, data)
    local prompt = (target == player) and "#ol_hc__zhendu-self" or "#ol_hc__zhendu-invoke::"..target.id
    local card = player.room:askForDiscard(player, 1, 1, false, self.name, true, ".", prompt, true)
    if #card > 0 then
      player.room:doIndicate(player.id, {target.id})
      self.cost_data = {tos = {target.id}, cards = card}
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:throwCard(self.cost_data.cards, self.name, player, player)
    if not target.dead and target:canUseTo(Fk:cloneCard("analeptic"), target) then
      room:useVirtualCard("analeptic", nil, target, target, self.name, false)
    end
    if player ~= target and not target.dead then
      room:damage{
        from = player,
        to = target,
        damage = 1,
        skillName = self.name,
      }
    end
  end,
}
local ol__qiluan = fk.CreateTriggerSkill{
  name = "ol_hc__qiluan",
  anim_type = "drawcard",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      local logic = player.room.logic
      local deathevents = logic.event_recorder[GameEvent.Death] or Util.DummyTable
      local turnevents = logic.event_recorder[GameEvent.Turn] or Util.DummyTable
      return #deathevents > 0 and #turnevents > 0 and deathevents[#deathevents].id > turnevents[#turnevents].id
    end
  end,
  on_use = function(self, event, target, player, data)
    local x = 0
    player.room.logic:getEventsOfScope(GameEvent.Death, 1, function (e)
      local deathData = e.data[1]
      if deathData.damage and deathData.damage.from == player then
        x = x + 3
      else
        x = x + 1
      end
      return false
    end, Player.HistoryTurn)
    if x > 0 then
      player:drawCards(x, self.name)
    end
  end,
}
hetaihou:addSkill(ol__zhendu)
hetaihou:addSkill(ol__qiluan)
Fk:loadTranslationTable{
  ["ol_hc_heg__n_hetaihou"] = "何太后",
  ["#ol_hc_heg__n_hetaihou"] = "弄权之蛇蝎",
  ["illustrator:ol_hc_heg__n_hetaihou"] = "KayaK&木美人",

  ["ol_hc__zhendu"] = "鸩毒",
  [":ol_hc__zhendu"] = "一名角色的出牌阶段开始时，你可以弃置一张手牌。若如此做，该角色视为使用一张【酒】，然后若其不为你，你对其造成1点伤害。",
  ["ol_hc__qiluan"] = "戚乱",
  [":ol_hc__qiluan"] = "一名角色回合结束时，你可摸X张牌（X为本回合死亡的角色数，其中每有一名角色是你杀死的，你多摸两张牌）。",
  ["#ol_hc__zhendu-invoke"] = "鸩毒：你可以弃置一张手牌视为 %dest 使用一张【酒】，然后你对其造成1点伤害",
  ["#ol_hc__zhendu-self"] = "鸩毒：你可以弃置一张手牌视为使用一张【酒】",

  ["$ol_hc__zhendu1"] = "怪只怪你，不该生有皇子！",
  ["$ol_hc__zhendu2"] = "后宫之中，岂有你的位置！",
  ["$ol_hc__qiluan1"] = "待我召吾兄入宫，谁敢不从？",
  ["$ol_hc__qiluan2"] = "本后自有哥哥在外照应，有什么好担心的！",
  ["~ol_hc_heg__n_hetaihou"] = "你们男人造的孽，非要说什么红颜祸水……",
}

return extension
